﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib.OS
{
	/// <summary>
	/// 待机管理
	/// </summary>
	/// <see cref="http://www.cnblogs.com/TianFang/archive/2012/10/12/2721883.html"/>
	public class SleepManager
	{
		/// <summary>
		///阻止系统休眠，直到线程结束恢复休眠策略
		/// </summary>
		/// <param name="includeDisplay">是否阻止关闭显示器</param>
		public static void PreventSleep(bool includeDisplay = false)
		{
			if (includeDisplay)
				SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display | ExecutionFlag.Continus);
			else
				SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Continus);
		}

		/// <summary>
		///恢复系统休眠策略
		/// </summary>
		public static void ResotreSleep()
		{
			SetThreadExecutionState(ExecutionFlag.Continus);
		}

		/// <summary>
		///重置系统休眠计时器
		/// </summary>
		/// <param name="includeDisplay">是否阻止关闭显示器</param>
		public static void ResetSleepTimer(bool includeDisplay = false)
		{
			if (includeDisplay)
				SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display);
			else
				SetThreadExecutionState(ExecutionFlag.System);
		}

		#region WinAPI

		//定义API函数
		[DllImport("kernel32.dll")]
		static extern uint SetThreadExecutionState(ExecutionFlag flags);

		[Flags]
		enum ExecutionFlag : uint
		{
			System = 0x00000001,
			Display = 0x00000002,
			Continus = 0x80000000,
		}

		#endregion
	}
}
